﻿
var comForm = {
    // 表单设置值(下拉值)
    bind: function (form, name, dataobj, optionType) {
        // 先清空
        if (form) {
            form.bind(name, dataobj, optionType);
        } else {
            var listControl = document.getElementById(name);
            if (listControl) {
                listControl.length = 0;
                listControl = $("#" + name);

                if (!optionType || (optionType && optionType == 2)) {
                    var o = {};
                    o[dataobj.valueField || "Key"] = "";
                    o[dataobj.textField || "Value"] = lang.all;
                    dataobj.data.unshift(o);
                } else if (optionType == 1) {
                    var o = {};
                    o[dataobj.valueField || "Key"] = "";
                    o[dataobj.textField || "Value"] = lang.Please_select;
                    dataobj.data.unshift(o);
                }
                _bindSelect(listControl, dataobj.data, dataobj.textField, dataobj.valueField);
            }
        }
    },
    datetime: function (form, name, value) {
        if (value) {
            if (form) {
                form.setItemValue(name, value);
            } else {
                //$("#" + name).val(value);
                $("#" + name).datebox('setValue', value);
            }
        } else {
            if (form) {
                return form.getItemValue(name, value);
            } else {
                return $("#" + name).datebox('getValue');
            }
        }
    }
}

function _bindSelect(obj, opts, text, value) {
    if (!obj || !opts) return;
    if (obj.jquery)
        obj = obj[0];
    for (var q = 0; q < opts.length; q++) {
        var opt = new Option(opts[q][text || 'Value'], opts[q][value || 'Key'] || '');
        if (obj.options)
            obj.options.add(opt);
        if (opts[q].selected == true || opts[q].selected == "true") {
            opt.selected = true;
        }
    }
}

var toolbarBtn = {
    openConn: { text: lang.open_query_condition, click: itemclick, icon: 'check', toggle: true },
    // 选中所有页
    checkall: { text: lang.select_all_pages, click: itemclick, icon: 'check', toggle: true },
    addbutton: { text: lang.add, click: itemclick, icon: 'add' },
    editbutton: { text: lang.modify, click: itemclick, icon: 'edit' },
    addDicttype: { text: lang.add_new_type, click: function () { openPage("addDicttype", "", "", lang.add_new_type); }, icon: 'add' },
    addDictitem: { text: lang.add_new_element, click: function () { openPage("dictitemedit", "dicttypeid=" + $.getPara('dicttypeid'), "", lang.add_new_element); }, icon: 'add' },
    addExttype: { text: lang.add_new_type, click: function () { openPage("addExttype", "", "", lang.add_new_type); }, icon: 'add' },
    editExttype: { text: lang.add_new_type, click: function () { openPage("editExttype", "", "", lang.add_new_type); }, icon: 'add' },
    addExtitem: { text: lang.add_new_element, click: function () { openPage("extitemedit", "dicttypeid=" + $.getPara('dicttypeid'), "", lang.add_new_element); }, icon: 'add' },
    structTableProEdit: { text: lang.add_new_type, click: function () { openPage("structTableProEdit", "", "", lang.add_new_type); }, icon: 'add' }


}

// 工具条方法
function itemclick(e) {
    var btn = e.data.sender;
    var grid = btn.grid;
    // 如果按钮灰色退出
    if ($(e.data.input).linkbutton('options').disabled) return;
    // 当前页参数传替更改
    var para = $.getPara('p') || '';
    para = "objstr=" + para.split('|')[1];

    switch (btn.text) {
        case "选中所有页":
            grid.checkAll(e.data.input);
            break;
        case "发起计划":
            if (!gridcheck(grid, true)) return;
            if (para.indexOf("futures") != -1)
                para += "&customertype=futures";
            openPage("launchplan", para, "", "发起计划");
            break;
        case "新增分组":
            openPage("groupadd", para, "", "新增分组");
            break;
        case "统一通讯":
            if (!gridcheck(grid, true)) return;
            if (para.indexOf("futures") != -1)
                para += "&customertype=futures";
            var docId = $.getPara("docId");
            if (docId && docId != "")
                para += "&docId=" + docId;
            openPage("communicate", para, "", "统一通讯");
            break;

    }
}
// 打开新页面
function openPage(type, para, id, text) {
    if (type == 'customerDetail_') {
        _openTab('customerDetail_' + id, App_Config.CurrentUrl + '/web/customer/customerdetail.html?' + para, text);
    } else if (type == 'menuedit') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|menuedit&' + para, text || lang.edit, 350, 360);
    } else if (type == 'roleedit') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|roleedit&' + para, text || lang.edit, 370, 310);
    } else if (type == 'organizationedit') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|organizationedit&' + para, text || lang.edit, 400, 365);
    } else if (type == 'useredit') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/useredit.html?' + para, text || lang.edit, 530, 420);
    } else if (type == 'rolerights') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/rolerights.html?' + para, text || lang.assignment_pemission, 450, 420);
    } else if (type == 'roleopraright') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/roleopraright.html?' + para, text || lang.Distribution_file_operation_pemission, 360, 240);
    } else if (type == 'treeobjedit') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|treeobjedit&' + para, text || lang.tree_edit, 400, 260);
    } else if (type == 'roletree') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/roletree.html?' + para, text || lang.assignment_object_tree, 450, 420);
    } else if (type == 'teamedit') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/teamedit.html?' + para, text || lang.edit, 560, 440);
    } else if (type == 'userselect') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/userselect.html?' + para, text || lang.group_members_manage, 510, 450);
    } else if (type == 'dataAdminUserSelect') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/dataAdminUserSelect.html?' + para, text || lang.group_members_manage, 510, 450);
    } else if (type == 'addDicttype') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|dictedit&' + para, text || lang.edit, 350, 190);
    }else if (type == 'dictitemlist') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|dictitemlist&' + para, text || lang.details, 620, 430);
    } else if (type == 'dictitemedit') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|dictitemedit&' + para, text || lang.add, 340, 240);
    } else if (type == 'rolestructright') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|rolestructright&' + para, text || lang.Distribution_table_operation_pemission, 700, 420);
    } else if (type == 'userinfo') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|usermanage&' + para, text || lang.user_information, 600, 600);
    } else if (type == 'pwsmanage') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|pwsmanage&' + para, text || lang.modify_password, 450, 260);
    } else if (type == 'feedback') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/feedback.html?p=basesys|feedback&' + para, text || lang.feedback, 490, 340);
    } else if (type == 'extitemlist') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|extitemlist&' + para, text || lang.details, 500, 400);
    } else if (type == 'extitemedit') {
        _openPage(type, App_Config.CurrentUrl + '/main.html?p=basesys|extitemedit&' + para, text || lang.add, 300, 180);
    } else if (type == 'exttypeedit') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/exttypeedit.htm?' + para, text || lang.edit, 500, 300);
    } else if (type == 'addExttype') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/exttypeadd.htm?' + para, text || lang.edit, 530, 350);
    } else if (type == 'editExttype') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/exttypeedit.htm?' + para, text || lang.edit, 530, 300);
    } else if (type == 'structTableProEdit') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/structTableProEdit.htm?' + para, text || lang.edit, 450, 300);
    } else if (type == 'themeEdit') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/themeEdit.htm?' + para, text || lang.edit, 570, 340);
    } else if (type == 'themeselect') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/themeselect.htm?' + para, text || lang.SUBJ_CLASS_NAME, 330, 400);
    }else if (type == 'themeTypeEdit') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/themeTypeEdit.htm?' + para, text || lang.edit, 350, 200);
    } else if (type == 'themeTypeSelect') {
        _openPage(type, App_Config.CurrentUrl + '/webhtml/themeTypeSelect.htm?' + para, text || lang.group_members_manage, 330, 400);
    }


}


function _openTab(_name, _url, _text) {
    parent.addTab(_text, _url, _name, 0);
}
function _openPage(_name, _url, _text, _width, _height, _id, _config) {
    if ($.getPara('outid') != '') {
        var outidstr = '';
        outidstr = 'outid=' + $.getPara('outid');
        if (_url.indexOf('?') > -1)
            _url = _url + "&" + outidstr;
        else
            _url = _url + "?" + outidstr;
    }

    $.openPage(_name, $.cookieUrl(_url), _text, _width, _height, _id, _config);
}





//#region 生成下载地址 
function downloadurl(type, attachurl, attachname) {
    return App_Config.TadongUrl + '/download.aspx?type=' + type + '&attach=' + attachurl + '&attachname=' + attachname;
}
//#endregion




//function _closePage(_id) {
//    $.fancybox.close();
//}